/*
 *  xycloud/lhdfs/src/write.c
 *
 *  (C) 2011 Harry Wei
 */

#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "../include/error.h"
#include "../include/lhdfs.h"

/*
 * lhdfs_write: write the buff sized len to offset pos.
 * @ctrl: the global control in RAM.
 * @buf: the data stores in buf in RAM.
 * @len: the data written to DISK in the length of len.
 * @pos: the data written to DISK with the offset pos in a segment.
 */

int 
lhdfs_write(struct lhdfs_ctrl *ctrl, 
		char *buf, size_t len, off_t pos)
{
	uint32_t ib = 0;
	uint32_t db_ino = 0;
	uint32_t db_storage_address = 0;

	if (LHDFS_SEGSIZE < ctrl->last_offset) {
		if (create_new_segment_file(ctrl->last_segment + 1))
			return 1;
		ctrl->last_offset = 0;
	}
	if (0 != pos % MAXDBSIZE) {
		db_ino = pos / MAXDBSIZE;
		if (is_db_level1_index_range(db_ino))
			db_storage_address = (ctrl->inode + (sizeof(uint32_t) << 1) 
						+ (sizeof(uint64_t) << ((MAXSEGOFFSET >> 1) + 1)));
		else if (is_db_level2_index_range(db_ino)) {
			ib = (ctrl->inode + )
		}
	}
}
